package com.example.yijian.edit.view.popup;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.yijian.R;
import com.example.yijian.edit.clipEdit.CurveSpeedUtil;
import com.example.yijian.edit.data.ChangeSpeedCurveInfo;
import com.example.yijian.edit.view.EditChangeSpeedCurveView;
import com.example.yijian.utils.LogUtils;
import com.meicam.sdk.NvsStreamingContext;
import com.meicam.sdk.NvsVideoClip;

import java.util.List;
import java.util.logging.Logger;

public class SpeedCurveEditPopup extends BaseCommonPopupWindow {

    private static final Object TAG = "SpeedCurveEditPopup";
    private List<ChangeSpeedCurveInfo> changeSpeedCurveInfoList;
    private EditChangeSpeedCurveView editChangeSpeedCurveView;
    private TextView tvCurveReset;
    private TextView tvCurveName;
    private ImageView ivCurveConfirm;

    private ChangeSpeedCurveInfo changeSpeedCurveInfo;
    private NvsVideoClip videoClip;

    public SpeedCurveEditPopup(Context context) {
        super(context);
        setContentView(R.layout.popup_edit_curve_speed);
        initView();
    }

    private void initView() {
        changeSpeedCurveInfoList = CurveSpeedUtil.listSpeedFromJson(getContext());
        /*编辑曲线变速*/
        editChangeSpeedCurveView = findViewById(R.id.change_speed_curve_view);
        tvCurveReset = findViewById(R.id.tv_curve_reset);
        tvCurveName = findViewById(R.id.tv_curve_name);
        ivCurveConfirm = findViewById(R.id.iv_curve_confirm);
        tvCurveReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editChangeSpeedCurveView.setInfo(changeSpeedCurveInfoList.get(changeSpeedCurveInfo.index));
                if (onFunctionListener != null) {
                    onFunctionListener.onSpeedChanged(changeSpeedCurveInfoList.get(changeSpeedCurveInfo.index), 0);
                    onFunctionListener.onActionUp(0);
                }
            }
        });
        ivCurveConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        editChangeSpeedCurveView.setOnFunctionListener(new EditChangeSpeedCurveView.OnFunctionListener() {
            @Override
            public void onChangePoint(boolean addPoint) {
                LogUtils.e(TAG, "onChangePoint");
                if (onFunctionListener != null) {
                    onFunctionListener.onStopVideo();
                }
            }

            @Override
            public void onTimelineMove(long timePoint) {
                LogUtils.e(TAG, "onTimelineMove");
//                if (onFunctionListener != null) {
//                    onFunctionListener.onActionUp(timePoint);
//                }

            }

            @Override
            public void onSpeedChanged(String speed, long timePoint) {
                LogUtils.e(TAG, "onSpeedChanged");
                changeSpeedCurveInfo.speed = speed;
                if (onFunctionListener != null) {
                    onFunctionListener.onSpeedChanged(changeSpeedCurveInfo, timePoint);
                }

            }

            @Override
            public void onActionDown() {
                LogUtils.e(TAG, "onActionDown");
            }

            @Override
            public void onActionUp(long timePoint) {
                LogUtils.e(TAG, "onActionUp");
                if (onFunctionListener != null) {
                    onFunctionListener.onActionUp(timePoint);
                }
            }

            @Override
            public void onSelectPoint() {
                LogUtils.e(TAG, "onSelectPoint");
                if (onFunctionListener != null) {
                    onFunctionListener.onStopVideo();
                }
            }
        });
    }

    public void setData(ChangeSpeedCurveInfo changeSpeedCurveInfo, NvsVideoClip videoClip) {
        this.changeSpeedCurveInfo = changeSpeedCurveInfo;
        this.videoClip = videoClip;
        editChangeSpeedCurveView.post(() -> {
            editChangeSpeedCurveView.setInfo(changeSpeedCurveInfo);
            editChangeSpeedCurveView.setClipDuration(videoClip.getTrimOut() - videoClip.getTrimIn());
            tvCurveName.setText(changeSpeedCurveInfo.mName);
        });
    }

    public void updatePlayProgress(long bzPosition) {
        if (!TextUtils.isEmpty(changeSpeedCurveInfo.speed)) {
            bzPosition = videoClip.GetTimelinePosByClipPosCurvesVariableSpeed(bzPosition) - videoClip.getTrimIn();
        }
        editChangeSpeedCurveView.upDataPlayProgress(bzPosition);
    }


    private OnFunctionListener onFunctionListener;

    public void setOnFunctionListener(OnFunctionListener callBack) {
        this.onFunctionListener = callBack;
    }


    public interface OnFunctionListener {
        //        void onConfirm(float speed, boolean changeVoice);
//
        void onStopVideo();

        void onTimelineMove(long timePoint);

        void onActionUp(long timePoint);

        void onSpeedChanged(ChangeSpeedCurveInfo speed, long timePoint);
    }


}
